package com.health.httpbean.upload;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UiMessageUtils;
import com.blankj.utilcode.util.Utils;
import com.health.httpbean.HttpConstant;
import com.health.httpbean.HttpDataBean;
import com.health.httpbean.HttpNewConstant;
import com.health.httpbean.HttpResultBean;
import com.health.httpbean.QueryTypeEnum;
import com.health.httpbean.format.FormatFjhData;
import com.health.httpbean.model.ArchiveBean;
import com.health.httpbean.model.UserInfoBean;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_TUBERCULOSIS_FOLLOWUP;
import com.health.tools.GlobalConts;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.settings.DataDownLoadMainActivity;

import java.util.List;

import rxhttp.RxHttp;

/**
 * author : xiaoxin
 * date   : 2020/12/19 19:02
 * desc   :
 * update :
 */
public class ArchiveUpload {

    private static final String TAG = "ArchiveUpload";

//    private Activity activity;

    public static List<ARCHIVE_BASEINFO> searchDbData() {
//        this.activity = activity;

        // IS_UPDATE_NEW = 'Y' OR IS_UPDATE_NEW IS NULL
        List<ARCHIVE_BASEINFO> list = NovaApplication.fdbmanger.findAllByWhere(ARCHIVE_BASEINFO.class,
                null);
        return list;
    }

    private static void upLoadHttp(ARCHIVE_BASEINFO model) {
//        RxHttp.postJson("https://183.66.66.49:8081/moon-api/api/personnel/saveOrUpdatePersonal")
//        RxHttp.postJson(HttpNewConstant.defaultURl+"/api/personnel/saveOrUpdatePersonal")
//                .addAll(GsonUtils.toJson(formatArchive(model)))
//                .asClass(HttpResultBean.class)
//                .subscribe(resultBean -> {
//                    LogUtils.e(resultBean);
//                    if (resultBean.getResponseCode() == 0) {
//                        model.setIS_UPDATE_NEW("N");
//                        upDataTable(model);
//                    } else {
//                        ToastUtils.showShort("" + resultBean.getMsg());
//                    }
//                }, throwable -> {
//                    LogUtils.e(throwable.getMessage());
//                });
    }

    public static void upDataTable(ARCHIVE_BASEINFO model) {
        model.setIS_UPDATE_NEW("N");
        NovaApplication.fdbmanger.updata(model);
    }

    /**
     * 构造新平台档案数据
     * @param archiveBaseinfo
     * @return
     */
    public static ArchiveBean formatArchive(ARCHIVE_BASEINFO archiveBaseinfo) {
        if (archiveBaseinfo == null) {
            return null;
        }
        ArchiveBean archiveBean = null;
        try {
            archiveBean = new ArchiveBean();
            archiveBean.setName(archiveBaseinfo.getCUSTOMERNAME());
            archiveBean.setIdCard(archiveBaseinfo.getIDCARD());
            try {
                archiveBean.setBirthday(TimeUtils.date2String(archiveBaseinfo.getBIRTHDAY(),"yyyy-MM-dd"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            if ("DM01-08_1".equals(archiveBaseinfo.getSEX())) {
                archiveBean.setGender(1);
            } else {
                archiveBean.setGender(2);
            }
            archiveBean.setMacdz(GlobalConts.generateIMEI(Utils.getApp()));
            archiveBean.setSelfPhone(archiveBaseinfo.getPHONE());
            archiveBean.setAddress(archiveBaseinfo.getADDR());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return archiveBean;
    }

    public static void uploadArchiveBaseInfo() {
        try {
            if (!GlobalConts.isNetworkAvailable(Utils.getApp())) {
                // showTextDialog("请先检查网络");
                return;
            }
            List<ARCHIVE_BASEINFO> baseinfos = searchDbData();
            if (baseinfos == null || baseinfos.size() <= 0) {
                return;
            }
            for (int i = 0; i < baseinfos.size(); i++) {
                upLoadHttp(baseinfos.get(i),i, baseinfos.size() - 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void upLoadHttp(ARCHIVE_BASEINFO model, int position, int total) {
        RxHttp.postJson(HttpNewConstant.getHttpNewBaseUrl() + HttpNewConstant.saveOrUpdatePersonal)
                .addAll(GsonUtils.toJson(formatArchive(model)))
                .asClass(HttpResultBean.class)
                .subscribe(resultBean -> {
                    LogUtils.e(resultBean);
                    if (resultBean.isOk()) {
                        upDataTable(model);
                    } else {
//                        ToastUtils.showShort("" + resultBean.getMsg());
                    }
//                    if (position == total) {
//                        ServiceUtils.stopService(UploadService.class);
//                    }
                }, throwable -> {
                    LogUtils.e(throwable.getMessage());
                });
    }

}
